package com.anjubao.parking.municipal.parkinglot.service;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.parkinglot.constant.ParkingStatusEnum;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotReturnCodeDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotStatusDto;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotStatusMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotStatus;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 设备日志 service层
 *
 */
@Slf4j
@Service
public class ParkingLotStatusService extends ServiceImpl<ParkingLotStatusMapper, ParkingLotStatus> {

    @Resource
    private ParkingLotStatusMapper parkingLotStatusMapper;



    @Value("${ParkingLots.less:0.3}")
    //车位不足
    private String parkingLotsLessPercent;


    @Value("${ParkingLots.full:0.1}")
    //车位已满
    private String parkingLotsFullPercent;

    public IPage<ParkingLotStatusDto> getStatusList(Page<ParkingLotStatusDto> page, Integer cityId) {
         IPage<ParkingLotStatusDto> res=parkingLotStatusMapper.getStatusList(page,cityId);
         for(ParkingLotStatusDto parkingLotStatusDto:res.getRecords()){
                Float freeOfTotal= Float.valueOf(parkingLotStatusDto.getFreeSlots())/Float.valueOf(parkingLotStatusDto.getTotalSlots());
                if(freeOfTotal<Float.valueOf(parkingLotsFullPercent)){
                    parkingLotStatusDto.setParkingLotStatus(ParkingStatusEnum.ParkingLotsFull.getDesc());
                }else  if(freeOfTotal>Float.valueOf(parkingLotsLessPercent)){
                    parkingLotStatusDto.setParkingLotStatus(ParkingStatusEnum.ParkingLotsMore.getDesc());
                }else {
                    parkingLotStatusDto.setParkingLotStatus(ParkingStatusEnum.ParkingLotsLess.getDesc());
                }
         }
         return res;
    }

    public List<ParkingLotStatusDto> getSlotStatByParkingLot(List<String> parkingLotIds) {
        return parkingLotStatusMapper.getSlotStatByParkingLot(parkingLotIds);
    }

    /**
     * 修改停车场空闲车位信息
     *
     * @param parkingLotCode
     * @param freeSlots
     * @return
     */
    public ParkingLotReturnCodeDto updateParkingLotFree(String parkingLotCode, Integer freeSlots) {
        ParkingLotStatus parkingLotStatus = parkingLotStatusMapper.selectById(MunicipalParkingUtils.getIdByCode(parkingLotCode));
        parkingLotStatus.setFreeSlots(parkingLotStatus.getFreeSlots()+freeSlots);
        parkingLotStatusMapper.updateById(parkingLotStatus);
        return new ParkingLotReturnCodeDto(parkingLotStatus.getId().toString());
    }

    /**
     * 批量更新停车场空闲车位信息
     * @param parkingLotStatusDtoList
     * @return
     */
    public Integer updateFreeList(List<ParkingLotStatusDto> parkingLotStatusDtoList) {
        int integer=0;
        for (ParkingLotStatusDto parkingLotStatusDto : parkingLotStatusDtoList) {
            integer=parkingLotStatusMapper.updateFreeList(parkingLotStatusDto);
        }
        return integer;
    }
}
